package cn.hg.jbsa.admin.directive.system;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.sql.JbsaDirectiveBase;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import cn.hg.jbsa.admin.model.JbsaOrg;
import cn.hg.jbsa.admin.provider.JbsaOrgServiceProvider;
import cn.hg.jbsa.common.constant.JbsaConstants;
import cn.hutool.core.text.CharSequenceUtil;
import io.jboot.db.model.Columns;
import io.jboot.utils.CacheUtil;
import io.jboot.web.directive.annotation.JFinalDirective;

/**
 * @author 胡高
 * @date 2021/12/10
 */
@JFinalDirective(OrgDirective.DIRECTIVE_NAME)
public class OrgDirective extends JbsaDirectiveBase {

    public static final String DIRECTIVE_NAME = "orgList";

    @Inject
    JbsaOrgServiceProvider orgServiceProvider;

    @Override
    public boolean hasEnd() {
        return true;
    }

    /* (non-Javadoc)
     * @see io.jboot.web.directive.base.JbootDirectiveBase#onRender(com.jfinal.template.Env, com.jfinal.template.stat.Scope, com.jfinal.template.io.Writer)
     */
    @Override
    public void onRender(Env env, Scope scope, Writer writer) {
        String var = this.getPara("var", scope);

        List<JbsaOrg> partnerList = CacheUtil.get(JbsaConstants.CACHE_NAME_ORG, "all",
            () -> OrgDirective.this.orgServiceProvider.findListByColumns(Columns.create("status", "on")),
            JbsaConstants.CACHE_LIVE_SECONDS);

        scope.set(CharSequenceUtil.isNotBlank(var) ? var : DIRECTIVE_NAME, partnerList);

        this.renderBody(env, scope, writer);
    }
}
